package com.ling.controller.admin;

import com.ling.bean.Admin;
import com.ling.bean.Msg;
import com.ling.service.admin.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class AdminLoginController {
    private final AdminService adminService;

    @Autowired
    public AdminLoginController(AdminService adminService) {
        this.adminService = adminService;
    }

    //跳转到登录页面
    @RequestMapping(value = "/adminLogin", method = RequestMethod.GET)
    public String goToAdminLogin() {
        return "admin/login";
    }

    @RequestMapping(value = "/adminExit", method = RequestMethod.GET)
    public String adminExit(@Autowired HttpSession session) {
        session.setAttribute("admin",null);
        return "admin/login";
    }



    //管理员登录
    @RequestMapping(value = "/adminLogin", method = RequestMethod.POST)
    @ResponseBody
    public Msg AdminLogin(Admin admin, @Autowired HttpSession session) {
        if (adminService.checkAdmin(admin)) {
            //可以登录,保存到session中
            session.setAttribute("admin",adminService.getAdmin(admin));
            return Msg.success();
        } else {
            //不能登录
            return Msg.fail().add("error", "用户名和密码不匹配，请重试！！");
        }
    }
}
